﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Marbles.Core.Connectors;
using Marbles.Core.Content.Sites;
using System.Data;

namespace Marbles.Core.Content.Users
{
    public class UserCollection : ICollection<User>
    {

        public int Count
        {
            get { return _items.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public User this[int index] { get { return _items[index]; } set { _items[index] = value; } }

        private List<User> _items = null;

        public UserCollection()
        {
            _items = new List<User>();
        }

        public UserCollection(string SiteId, bool excluderoot)
            : this()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = null;

            if (excluderoot)
            {
                cmd = new Command("SELECT DISTINCT a.* FROM Users a INNER JOIN usergrouplink b ON a.userid = b.userid AND a.siteid = b.siteid AND b.groupid <> 4 WHERE a.siteid = @siteid ORDER BY a.fullname ASC");
            }
            else
            {
                cmd = new Command("SELECT * FROM Users WHERE siteid = @siteid ORDER BY fullname ASC");
            }
            cmd.AddParameter("@siteid", SiteId);
            foreach (DataRow dr in dbc.FetchDataTable(cmd).Rows)
            {
                _items.Add(new User(dr));
            }
        }


        public static bool Exists(string SiteId, string Username)
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT userid FROM Users WHERE siteid = @siteid AND username = @username");
            DataTable dt = dbc.FetchDataTable(cmd);
            if (dt.Rows.Count > 0) { return true; }
            return false;
        }

        public static bool Exists(string Username)
        {
            return Exists(SiteDefinition.GetCurrent().SiteId, Username);
        }

        public void Add(User item)
        {
            _items.Add(item);
        }

        public void Clear()
        {
            _items.Clear();
        }

        public bool Contains(User item)
        {
            return _items.Contains(item);
        }

        public void CopyTo(User[] array, int arrayIndex)
        {
            _items.CopyTo(array, arrayIndex);
        }

        public bool Remove(User item)
        {
            return _items.Remove(item);
        }

        public IEnumerator<User> GetEnumerator()
        {
            return _items.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _items.GetEnumerator();
        }

        public static Dictionary<Int32, string> GetGroups(bool excluderoot)
        {
            Dictionary<Int32, string> items = new Dictionary<int, string>();
            DataConnection dbc = new DataConnection();
            Command cmd = null;

            if (excluderoot)
            {
                cmd = new Command("SELECT groupid, title FROM usergroups WHERE title <> 'Root' ORDER BY title ASC");
            }
            else
            {
                cmd = new Command("SELECT groupid, title FROM usergroups ORDER BY title ASC");
            }

            DataTable dt = dbc.FetchDataTable(cmd);
            foreach (DataRow dr in dt.Rows)
            {
                items.Add(Convert.ToInt32(dr["groupid"]), dr["title"].ToString());
            }
            return items;

        }
    }
}